import { uuid } from '@/utils/util'

//生成1000000内的随机数，用于生成组件的唯一标识$$index
export const genComponentIndex = (component_index = []) => {
  let key = Math.floor(Math.random() * 1000000)
  if (component_index.includes(key)) {
    key = this.genComponentIndex(component_index)
  }
  return key
}
export const getNodeApprovalType = function(type) {
  let detailLabel = ''
  switch (type) {
    case '1':
      detailLabel = this.LANG.OPTIONAL || '用户自选'
      break
    case '2':
      detailLabel = this.LANG.DESIGNATED_PERSON || '指定人员'
      break
    case '3':
      detailLabel = this.LANG.SAME_AS_BEFORE || '与前置节点相同'
      break
    case '4':
      detailLabel = '指定岗位'
      break
    default:
      break
  }
  return detailLabel
}
// 标准审批节点
export const createApproval = (_this) => {
  return nodeCreator(_this, 'approval')
}

/**
 * @description: 标准分支（branch）
 * @param {index}  已有分支个数
 */
export const createBranch = (_this, index) => {
  return nodeCreator(_this, 'branch', _this.LANG.CONDITION + index)
}

// 添加标准路由节点
export const createRoute = (_this) => {
  return nodeCreator(_this, 'route', _this.LANG.CONDITION + '1')
}
/**
 * @description:用于创建自由流程中各个节点
 * @param {type} 节点类型
 * @param {title} 节点标题（仅用于分支节点与路由节点）
 */

export const nodeCreator = (_this, type, title) => {
  let tempNode = {}
  switch (type) {
    case 'approval':
      tempNode = {
        id: 'approval_' + uuid(16, 16),
        type: 'approval',
        title: _this.LANG.NODE_NAME || '节点名称',
        nodeConfig: {
          approvalType: '1',
          assignee: '',
          assigneeUserFullName: '',
          assigneeType: '1',
          assigneeLabel: '审批人描述',
          buttonList: JSON.stringify([
            buttonEnum._Object.submit,
            buttonEnum._Object.reject
          ]),
          required: '0',
          carbonCopyList: '', // 抄送人数组
          copyOnAgree: '',
          emptySolution: '0',
          emptyAssigneeUserName: '',
          emptyAssigneeUserFullName: '',
          formPermission: {},
          circulationParam: {
            notify: '',
            receiverUserFullName: '',
            receiverUserName: ''
          },
          dueDateParam: {
            handingType: 'none',
            day: 0,
            hour: 0,
            assignee: { userName: '', userFullname: '' },
            notify: ''
          }
        }
      }
      break
    case 'start':
      tempNode = {
        id: 'fawkes_custom_flow_start',
        type: 'start',
        title: '发起',
        nodeConfig: {
          approvalType: '1',
          assignee: '',
          assigneeUserFullName: '',
          assigneeType: '1',
          assigneeLabel: '发起人',
          buttonList: JSON.stringify([
            buttonEnum._Object.submit,
            buttonEnum._Object.abandon,
            buttonEnum._Object.saveDraft
          ]),
          required: '1',
          carbonCopyList: '', // 抄送人数组
          copyOnAgree: '',
          formPermission: {}
        }
      }
      break
    case 'end':
      tempNode = {
        id: 'end_' + uuid(16, 16),
        type: 'end',
        title: '结束',
        nodeConfig: {}
      }
      break
    case 'condition':
      tempNode = {
        id: 'condition_' + uuid(16, 16),
        type: 'condition',
        title: title || _this.LANG.CONDITION || '条件',
        condition: '', // 最终生成的条件字符串
        conditionConfig: '' // 用于展示条件的对象
      }
      break
    case 'branch':
      tempNode = {
        id: 'branch_' + uuid(16, 16),
        sort: 1,
        type: 'branch',
        direction: '',
        nodes: [
          nodeCreator(_this, 'condition', title),
          nodeCreator(_this, 'approval')
        ]
      }
      break
    case 'route':
      tempNode = {
        id: 'route_' + uuid(16, 16),
        type: 'route',
        title: '路由节点',
        branches: [
          nodeCreator(_this, 'branch', title),
          nodeCreator(_this, 'branch', _this.LANG.DEFAULT)
        ]
      }
      break
    default:
      break
  }
  return tempNode
}

export const buttonEnum = {
  _Object: {
    submit: {
      type: 'submit',
      text: '同意',
      code: 'SUBMIT',
      customCode: 'SUBMIT'
    },
    saveDraft: {
      type: 'saveDraft',
      text: '暂存',
      code: 'SAVEDRAFT',
      customCode: 'SAVEDRAFT'
    },
    reject: {
      type: 'reject',
      text: '退回',
      code: 'REJECT',
      customCode: 'REJECT',
      targetNode: 'fawkes_custom_flow_start'
    }, // 此退回按钮默认退回第一个节点
    abandon: {
      type: 'abandon',
      text: '废弃',
      code: 'ABANDON',
      customCode: 'ABANDON'
    },
    addCountersignee: {
      type: 'addCountersignee',
      text: '加签',
      code: 'ADDCOUNTERSIGNEE',
      customCode: 'ADDCOUNTERSIGNEE'
    },
    circulate: {
      type: 'circulate',
      text: '抄送',
      code: 'CIRCULATE',
      customCode: 'CIRCULATE'
    }
    // deleteCountersignee: { type: 'deleteCountersignee', text: '减签', code: 'DELETECOUNTERSIGNEE', customCode: 'DELETECOUNTERSIGNEE' }
  },
  _Array: [
    { type: 'submit', text: '同意', code: 'SUBMIT', customCode: 'SUBMIT' },
    {
      type: 'saveDraft',
      text: '暂存',
      code: 'SAVEDRAFT',
      customCode: 'SAVEDRAFT'
    },
    {
      type: 'reject',
      text: '退回',
      code: 'REJECT',
      customCode: 'REJECT',
      targetNode: 'fawkes_custom_flow_start'
    }, // 此退回按钮默认退回第一个节点
    { type: 'abandon', text: '废弃', code: 'ABANDON', customCode: 'ABANDON' },
    {
      type: 'addCountersignee',
      text: '加签',
      code: 'ADDCOUNTERSIGNEE',
      customCode: 'ADDCOUNTERSIGNEE'
    },
    {
      type: 'circulate',
      text: '抄送',
      code: 'CIRCULATE',
      customCode: 'CIRCULATE'
    }
    // { type: 'deleteCountersignee', text: '减签', code: 'DELETECOUNTERSIGNEE', customCode: 'DELETECOUNTERSIGNEE' }
  ]
}

// 流程键值prefix
export const custom_flow_prefix = 'fawkes_custom_flow_'
